package com.zsy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zsy.entity.PurchaseRequisition;
import com.zsy.mapper.IPurchaseRequisitionMapper;

import com.zsy.service.IPurchaseRequisitionsService;
import com.zsy.utils.PageBean;
import com.zsy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurchaseRequisitionsServiceImpl implements IPurchaseRequisitionsService {

    @Autowired
    private IPurchaseRequisitionMapper IPRMapper;

    @Override
    public PageBean<PurchaseRequisition> list(Integer pageNum,
                                              Integer pageSize,
                                              String purchaseId,
                                              String relevantData,
                                              String supplier) {
        // 1.创建PageBean对象
        PageBean<PurchaseRequisition> pb = new PageBean<>();
        // 2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);
        // 3.调用mapper
        //Map<String,Object> map = ThreadLocalUtil.get();
        //Integer userId = (Integer) map.get("id");
        List<PurchaseRequisition> as = IPRMapper.list(1,purchaseId, relevantData, supplier);
        // Page中提供了方法，可获取PageHelper分页查询后，得到的总条数和当前页数据
        Page<PurchaseRequisition> p = (Page<PurchaseRequisition>) as;
        // 把数据填到PageBean中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public Result add(PurchaseRequisition purchaseRequisition) {
        System.out.println(purchaseRequisition.toString());
        int add = IPRMapper.add(purchaseRequisition);
        if (add > 0) {
            return Result.ok("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    public Result deleteById(Integer id){
        if (id == null){
            return Result.error("id为空");
        }
        int i = IPRMapper.deleteById(id);
        if (i>0){
            return Result.ok("删除成功") ;
        }
        return Result.error("删除失败");
    }


    @Override
    public Result updateById(PurchaseRequisition purchaseRequisition) {
        System.out.println("service:>>"+purchaseRequisition.toString());
        if(purchaseRequisition.getPurchaseRequisitionsId() == null){
            return Result.error("主键id为空");
        }
        System.out.println(">>>>>>>>>>>>>>>>>");
        int i = IPRMapper.updateById(purchaseRequisition);
        if (i>0){
            return Result.ok("添加成功") ;
        }
        return Result.error("添加失败");
    }


}
